package com.example.learn.hwod;

import java.util.ArrayList;
import java.util.Scanner;

public class _07手机的注册时间 {
    static class App{
        String name;
        int priority;
        int startTime;
        int endTime;
        public App(String name, int priority, int startTime, int endTime){
            this.name = name;
            this.priority = priority;
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<App> apps = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            apps.add(new App(sc.next(), sc.nextInt(), convert(sc.next()), convert(sc.next())));
        }
        int queryTime = convert(sc.next());
        System.out.println(getResult(apps,queryTime));


    }

    private static String getResult(ArrayList<App> apps, int queryTime) {
        ArrayList<App> registeredList = new ArrayList<>();
        for (App app : apps) {
            if (app.startTime>app.endTime) {
                continue;
            }
            ArrayList<Integer> indexList = new ArrayList<>();
            for (int i = 0; i < registeredList.size(); i++) {
                App registered = registeredList.get(i);
                if(registered.startTime>=app.endTime||app.startTime>= registered.endTime)
                    continue;
                if (app.priority>registered.priority) {
                    indexList.add(i);
                }else continue;
            }


            registeredList.add(app);

        }

        return null;
    }

    public static int convert(String time){
        String[] split = time.split(":");
        return Integer.parseInt(split[0]) * 60 + Integer.parseInt(split[1]);
    }
}
